from django.db import models


class UserInfo(models.Model):
    """ 用户表 """
    phone = models.CharField(verbose_name='手机号', max_length=32)
    token = models.CharField(verbose_name='Token', max_length=32)
    openid = models.CharField(verbose_name="微信唯一标识", max_length=32)
    # session_key = models.CharField(max_length=64)


class User(models.Model):  # V3版本用到用户认证
    openid = models.CharField(max_length=128, unique=True)
    session_key = models.CharField(max_length=64)


class Goods(models.Model):
    """商品表"""
    title = models.CharField(verbose_name='商品名称', max_length=32)
    price = models.PositiveIntegerField(verbose_name="价格")


class Order(models.Model):
    """ 订单表 """
    status_choices = ((0, '待支付'), (1, '已支付'), (2, '一关闭'), (3, '已退款'))
    status = models.SmallIntegerField(verbose_name='状态', choices=status_choices)
    goods = models.ForeignKey(verbose_name='商品', to='Goods', on_delete=models.CASCADE)
    user = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.CASCADE)
    uid = models.CharField(verbose_name='订单号', max_length=64)


class OrderNew(models.Model):
    ORDER_STATUS = (
        ('0', '待支付'),
        ('1', '已支付'),
        ('2', '已取消'),
        ('3', '退款中'),
        ('4', '已退款'),
    )
    order_no = models.CharField(max_length=64, unique=True)
    user = models.ForeignKey('UserInfo', on_delete=models.CASCADE)
    total = models.DecimalField(max_digits=10, decimal_places=2)
    status = models.SmallIntegerField(choices=ORDER_STATUS, default='0')
    transaction_id = models.CharField(max_length=32, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    pay_time = models.DateTimeField(null=True)

